<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="default" :inline="true" label-width="68px">
      <el-form-item label="异议编号" prop="objectionNumber">
        <el-input
          v-model="queryParams.objectionNumber"
          placeholder="请输入异议编号"
          clearable
        />
      </el-form-item>
      <el-form-item label="处理日期" prop="handlingDate">
        <el-date-picker clearable
                        v-model="queryParams.handlingDate"
                        type="datetime"
                        format="YYYY-MM-DD HH:mm:ss"
                        value-format="YYYY-MM-DD HH:mm:ss"
                        placeholder="请选择处理日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-perms="['qa:objections:add']"
          v-preventReClick
        >新增
        </el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="objectionsList">
      <el-table-column label="主键id" align="center" prop="id" />
      <el-table-column label="异议编号" align="center" prop="objectionNumber" />
      <el-table-column label="异议人ID" align="center" prop="objectorId" />
      <el-table-column label="异议内容" align="center" prop="objectionContent" />
      <el-table-column label="状态" align="center" prop="status" />
      <el-table-column label="处理人ID" align="center" prop="handlerId" />
      <el-table-column label="处理意见" align="center" prop="handlingOpinion" />
      <el-table-column label="处理日期" align="center" prop="handlingDate" width="180" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button
            type="primary"
            link
            icon="Edit"
            @click="handleUpdate(scope.row)"
            v-perms="['qa:objections:edit']"
            v-preventReClick
          >修改
          </el-button>
          <el-button
            type="primary"
            link
            icon="Delete"
            @click="handleDelete(scope.row)"
            v-perms="['qa:objections:remove']"
            v-preventReClick
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      v-if="total>0"
      v-model:current-page="queryParams.pageNum"
      :page-size="10"
      layout="total, prev, pager, next"
      :total="total"
      style="float: right"
    />

    <!-- 添加或修改异议对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="异议编号" prop="objectionNumber">
          <el-input v-model="form.objectionNumber" placeholder="请输入异议编号" />
        </el-form-item>
        <el-form-item label="异议人ID" prop="objectorId">
          <el-input v-model="form.objectorId" placeholder="请输入异议人ID" />
        </el-form-item>
        <el-form-item label="异议内容" prop="objectionContent">
          <el-input v-model="form.objectionContent" type="textarea" placeholder="请输入异议内容" />
        </el-form-item>
        <template v-if="this.form.id !== null">
          <el-form-item label="处理人ID" prop="handlerId">
            <el-input v-model="form.handlerId" placeholder="请输入处理人ID" />
          </el-form-item>
          <el-form-item label="处理意见" prop="handlingOpinion">
            <el-input v-model="form.handlingOpinion" type="textarea" placeholder="请输入内容" />
          </el-form-item>
          <el-form-item label="处理日期" prop="handlingDate">
            <el-date-picker clearable
                            v-model="form.handlingDate"
                            type="datetime"
                            format="YYYY-MM-DD HH:mm:ss"
                            value-format="YYYY-MM-DD HH:mm:ss"
                            placeholder="请选择处理日期">
            </el-date-picker>
          </el-form-item>
        </template>
      </el-form>
      <template #footer>
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script>

import { ObjectionsService } from '@/api/qa/objections.js'

export default {
  name: 'Objections',
  data() {
    return {
      // 遮罩层
      loading: true,
      // 总条数
      total: 0,
      // 异议表格数据
      objectionsList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        objectionNumber: null,
        handlingDate: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        objectionNumber: [
          { required: true, message: '异议编号不能为空', trigger: 'blur' }
        ],
        objectorId: [
          { required: true, message: '异议人ID不能为空', trigger: 'blur' }
        ],
        objectionContent: [
          { required: true, message: '异议内容不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询异议列表 */
    getList() {
      this.loading = true
      ObjectionsService.listObjections(this.queryParams).then(response => {
        this.objectionsList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        objectionNumber: null,
        objectorId: null,
        objectionContent: null,
        status: null,
        handlerId: null,
        handlingOpinion: null,
        handlingDate: null,
        createTime: null,
        updateTime: null
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.$refs.queryForm.resetFields()
      this.handleQuery()
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.open = true
      this.title = '添加异议'
      this.reset()
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids
      ObjectionsService.getObjections(id).then(response => {
        this.form = response.data
        this.open = true
        this.title = '修改异议'
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            ObjectionsService.updateObjections(this.form).then(response => {
              this.$message.success('修改成功')
              this.open = false
              this.getList()
            })
          } else {
            ObjectionsService.addObjections(this.form).then(response => {
              this.$message.success('新增成功')
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$confirm('是否确认删除异议编号为"' + ids + '"的数据项？').then(function() {
        return ObjectionsService.delObjections(ids)
      }).then(() => {
        this.getList()
        this.$message.success('删除成功')
      }).catch(() => {
      })
    }
  }
}
</script>
